package com.example.siyun.service.impl.courseserviceimpl;

import com.example.siyun.mapper.coursemapper.TableMapper;
import com.example.siyun.pojo.*;
import com.example.siyun.service.courseservice.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description:教师模块教师列表的信息表格
 */
@Service
public class TableServiceImpl implements TableService {
    @Autowired
    private TableMapper tableMapper;
    /**
     * 获取课程列表数据
     * @return 课程列表数据
     */
    @Transactional
    @Override
    public List<CourseTable> findTable(CourseTable coursetable) {
        return tableMapper.findTable(coursetable);
    }
    /**
     * 获取下拉菜单数据
     * @param pid 父id
     * @return 返回下拉菜单的数据
     */
    @Transactional
    @Override
    public List<XiaTree> getXiaLa(Integer pid) {
        ArrayList<XiaTree> objects = new ArrayList<>();
        List<XiaTree> ej = tableMapper.getXiaLa(pid);
        for (XiaTree e:ej
        ) {
            XiaTree xl = new XiaTree();
            xl.setId(e.getId());
            xl.setTitle(e.getTitle());
            List<XiaTree> dh = getXiaLa(e.getId());
            xl.setChild(dh);
            objects.add(xl);
        }
        return objects;
    }
    /**
     * 通过id更新id的状态
     * @param id 要更新的课程id status要更新的状态
     */
    @Transactional
    @Override
    public void updateSwitch(int id, int status) {
        tableMapper.updateSwitch(id,status);
    }
    /**
     * 通过id查询对应的课程信息
     * @param id 要查询的课程id
     * @return 返回查询出来的课程信息
     */
    @Transactional
    @Override
    public CourseTable selectTable(int id) {
        return tableMapper.selectTable(id);
    }
    /**
     * 添加课程信息
     * @param coursetable 要添加的课程信息
     */
    @Transactional
    @Override
    public void addCourseTable(CourseTable coursetable) {
        tableMapper.addCourseTable(coursetable);
    }
    /**
     * 根据id删除对应的课程信息
     * @param id 要删除的要删除课程的id
     */
    @Transactional
    @Override
    public void delCourseTable(int id) {
        tableMapper.delCourseTable(id);
    }
    /**
     * 修改课程数据
     * @param coursetable 要修改的课程数据
     */
    @Transactional
    @Override
    public void editCourseTable(CourseTable coursetable) {
        tableMapper.editCourseTable(coursetable);
    }
    /**
     * 通过id查询对应的课程日志
     * @param logid 要查询的课程日志数据
     */
    @Transactional
    @Override
    public List<CourseLog> courseLog(int logid) {
        return tableMapper.courseLog(logid);
    }
    /**
     * 通过id获取课程价格
     * @param cid 要获取的课程id coursetable获取到的数据
     */
    @Override
    @Transactional
    public void getCoursePrice(int cid, CourseTable coursetable) {
        CourseTable coursetable1 = tableMapper.getCoursePrice(cid);
        CourseLog aa=new CourseLog();
        aa.setHisprice(coursetable1.getPrice());
        aa.setNowprice(coursetable.getPrice());
        aa.setBid(cid);
        aa.setName(coursetable.getName());
        aa.setAuditstatus(coursetable.getStatus());
        aa.setOperator("思云管理员");
        Timestamp time2 = new Timestamp(new Date().getTime());
        aa.setDatemodified(time2);
        tableMapper.insertEditLog(aa);
        tableMapper.editCourseTable(coursetable);
    }
    /**
     * 添加课程日志
     * @param courselog 要添加的课程日志信息
     */
    @Transactional
    @Override
    public void insertEditLog(CourseLog courselog) {
        tableMapper.insertEditLog(courselog);
    }

    @Override
    @Transactional
    public Map<String, Object> selectData() {
        Map map=new HashMap();
        Integer selectallcourse = tableMapper.selectAllCourse();
        Integer selectysj = tableMapper.selectYsj();
        Integer selectwsj = tableMapper.selectWsj();
        Integer selectdsh = tableMapper.selectDsh();
        Integer selectwtg = tableMapper.selectWtg();
        map.put("selectallcourse",selectallcourse);
        map.put("selectysj",selectysj);
        map.put("selectwsj",selectwsj);
        map.put("selectdsh",selectdsh);
        map.put("selectwtg",selectwtg);
        return map;
    }

}
